package gui;

import ge.escola.Serie;
import ge.escola.Turma;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AlteraTurma extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JTextField caixaSerie;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JTextField caixaNome;
	private JLabel checagemTurma;
	private JLabel checagemSerie;
	
	Color verde = new Color(0,150,0);
	private boolean flag = true;
	String nomeTurma,nomeSerie;
	private Turma turma;
	private JButton buttonOk;
	private JButton buttonHorario;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public AlteraTurma() {
		super();
		initGUI();
		turma = SelecionaTurma.turma;
		caixaNome.setText(turma.getNome());
		caixaSerie.setText(turma.getSerie().getNome());
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Alterar turma");
				jLabel1.setBounds(208, 20, 370, 54);
				jLabel1.setFont(new java.awt.Font("Arial",0,36));
				jLabel1.setForeground(new java.awt.Color(0,102,102));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				checagemSerie = new JLabel();
				this.add(checagemSerie);
				checagemSerie.setText("");
				checagemSerie.setFont(new java.awt.Font("Tahoma",0,14));
				checagemSerie.setForeground(Color.red);
				checagemSerie.setBounds(414, 303, 70, 21);
			}
			{
				checagemTurma = new JLabel();
				this.add(checagemTurma);
				checagemTurma.setText("");
				checagemTurma.setFont(new java.awt.Font("Tahoma",0,14));
				checagemTurma.setForeground(verde);
				checagemTurma.setBounds(418, 214, 66, 21);
			}
			{
				caixaSerie = new JTextField();
				this.add(caixaSerie);
				caixaSerie.setFont(new java.awt.Font("Tahoma",0,18));
				caixaSerie.setBounds(279, 296, 118, 28);
				caixaSerie.setForeground(new java.awt.Color(204,204,204));
				caixaSerie.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						caixaSerieKeyReleased(evt);
					}
				});
			}
			{
				caixaNome = new JTextField();
				this.add(caixaNome);
				caixaNome.setFont(new java.awt.Font("Tahoma",0,18));
				caixaNome.setBounds(278, 205, 118, 28);
				caixaNome.setForeground(new java.awt.Color(204,204,204));
				caixaNome.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						caixaNomeKeyReleased(evt);
					}
				});
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Nome da serie");
				jLabel3.setFont(new java.awt.Font("Tahoma",0,24));
				jLabel3.setForeground(new java.awt.Color(0,102,102));
				jLabel3.setBounds(102, 285, 159, 40);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Nome da turma");
				jLabel2.setFont(new java.awt.Font("Tahoma",0,24));
				jLabel2.setForeground(new java.awt.Color(0,102,102));
				jLabel2.setBounds(92, 195, 168, 40);
			}
			{
				buttonHorario = new JButton();
				this.add(buttonHorario);
				buttonHorario.setText("HORARIOS DA TURMA");
				buttonHorario.setBounds(539, 257, 129, 22);
				buttonHorario.setBackground(new java.awt.Color(255,255,255));
				buttonHorario.setFont(new java.awt.Font("Arial",0,10));
				buttonHorario.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonHorarioMouseClicked(evt);
					}
				});
			}
			{
				buttonOk = new JButton();
				this.add(buttonOk);
				buttonOk.setText("OK");
				buttonOk.setBounds(630, 561, 129, 22);
				buttonOk.setFont(new java.awt.Font("Arial",0,10));
				buttonOk.setBackground(new java.awt.Color(255,255,255));
				buttonOk.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonOkMouseClicked(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void caixaNomeKeyReleased(KeyEvent evt) {
		nomeTurma = caixaNome.getText();
		Pattern padrao = Pattern.compile("[A-Z]{1}");
        Matcher meta = padrao.matcher(nomeTurma);
		if(meta.matches()){
			checagemTurma.setForeground(verde);
			flag = true;
		}else{
			checagemTurma.setForeground(Color.red);
			checagemTurma.setText("INVALIDO");
			flag = false;
		}
	}
	
	private void caixaSerieKeyReleased(KeyEvent evt) {
		nomeSerie = caixaSerie.getText();
		if(Serie.validaNome(nomeSerie)){
			checagemSerie.setText("OK");
			checagemSerie.setForeground(verde);
			flag = true;
		}else{
			checagemSerie.setForeground(Color.red);
			flag = false;
		}
	}
	
	private void buttonOkMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.todasTurmas.remove(turma);
		if(capturaSerie() == null){
			JOptionPane.showMessageDialog(null, "Turma nao cadastrada.");
		}else{
			turma.setNome(nomeTurma);
			turma.setSerie(capturaSerie());
			GerenciadorDeJanelas.todasTurmas.add(turma);
			
			String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasTurmas);
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeTurmas,xml);
			
			GerenciadorDeJanelas.selecionaTurma = new SelecionaTurma();
	        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.selecionaTurma);
	        GerenciadorDeJanelas.window.setVisible(true);
		}
		
	}
	
	private void buttonHorarioMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.horarioTurma = new HorarioTurmaPanel();
		GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.horarioTurma);
        GerenciadorDeJanelas.window.setVisible(true);
	}
	
	private Serie capturaSerie(){
		for(Serie serie:GerenciadorDeJanelas.todasSeries){
			if(serie.getNome().equals(nomeSerie)){
				return serie;
			}
		}
		return null;
	}

}
